//
//  MovieViewController.m
//  iBaby365v2
//
//  Created by admin on 14/11/25.
//  Copyright (c) 2014年 suplus. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieCell.h"
#import <AncdaPlayer.h>
#import "MoviePlayViewController.h"
#import "MBProgressHUD+MJ.h"

@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate,MoviePlayViewControllerDelegate,AncdaSessionDelegate>

@property (strong, nonatomic) AncdaSession * m_session; // 平台会话

@property (strong, nonatomic) NSMutableArray *m_camraList;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) MoviePlayViewController *playerController;

@property(nonatomic,strong) AncdaCamera *m_camera;

@end

@implementation MovieViewController

-(MoviePlayViewController *)playerController
{
    if (!_playerController) {
        _playerController = [[MoviePlayViewController alloc]init];
        _playerController.m_session = self.m_session;
        _playerController.camera =self.m_camera;
        _playerController.delegate = self;
    }
    return _playerController;
}
- (void)moviePlayerDidFinished
{
    [self dismissViewControllerAnimated:YES completion:nil];
    self.playerController = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    self.tableView.delegate = self;

    [self login];

    // Do any additional setup after loading the view.
}
-(NSMutableArray *)m_camraList
{
    if (_m_camraList==nil) {
        _m_camraList = [NSMutableArray array];
    }
    return _m_camraList;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self){
        
    }
    return self;
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"摄像机个数%d",self.m_camraList.count);
    
    return self.m_camraList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog("ceshiwewewrerw");
    // 1.创建cell
    MovieCell *cell = [MovieCell cellWithTableView:tableView];
    NSLog(@"self.m_camraList-->%@",self.m_camraList[indexPath.row]) ;
    AncdaCamera *ac =self.m_camraList[indexPath.row];
    
    cell.textLabel.text = ac.name;
    

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.m_camera =self.m_camraList[indexPath.row];
    [self presentViewController:self.playerController animated:YES completion:nil];
}

#pragma mark - 监控相关代码

-(NSString*)GetErrorDesc:(long)wparam
{
    
    switch (wparam)
    {
        case RESULT_USER_EXIST:
            return @"用户已经在别处登录，请稍候再试！";
            break;
        case RESULT_PASSWORD_ERR:
            return @"用户名或者密码错误，请检查后再试！";
            
        case RESULT_DB_ERROR:
        case RESULT_MEMORY_ERROR:
        case RESULT_SSERVER_FAILED:
            return @"当前服务器负载过高，请检查后再试！";
            
        case RESULT_USER_EXPIRE:
            return @"用户已经过期，请进行续费！";
        case RESULT_VERSION_ERROR:
            return @"客户端版本错误，请重新下载安装！";
        case RESULT_USER_COUNT_MAX:
            return @"用户已经达到同时登录上限，请稍候重试！";
            
            break;
        default:
            return [NSString stringWithFormat:@"未预知的错误(%ld)，请稍候重试！",wparam];
            
            break;
    }
    
    
    
}

/**
 *
 *
 *
 */
- (NSMutableArray *)fillChildrenForNode:(NSString*)key
{
    
    NSArray * nodeArray = [self.m_session getNode:key]; //获取子节点
    NSArray *cameraArray = [self.m_session getCamera:key];//获取摄像机
    NSMutableArray *childArray = [NSMutableArray array ];
    
    
    for (AncdaCamera*item in cameraArray)
        
    {
        if(item.online == true){
            [childArray addObject:item];
            NSLog(@"摄像机名称:%@",item.name);
        }else{
            NSLog(@"摄像机不在线:%@",item.name);
        }
        
        
    }
    
    for (AncdaNode *item in nodeArray)
        
    {
        NSArray * child = [self fillChildrenForNode:item.key ]; //遍历子节点
        
        [childArray addObjectsFromArray:child];
        
        NSLog(@"节点名称:%@",item.name);
        
        
    }
    NSLog(@"----->>>>%d",childArray.count);
    return childArray;
    
}

-(void)loadAllCamera
{
    
    NSLog(@"-->%@",@"loadAllCamera");
    NSArray * all = [self fillChildrenForNode:@""];//从根节点遍历
    NSLog(@"-all.count->%d",all.count);
    [self.m_camraList removeAllObjects];
    [self.m_camraList addObjectsFromArray:all];
    NSLog(@"-->%d",self.self.m_camraList.count);
    
    [self.tableView reloadData];
    
}
//平台信息回调函数
-(void) OnAncdaSessionState:(int)state lparam:(long)lparam wparam:(long)wparam
{
    
    dispatch_async(dispatch_get_main_queue(), ^(void)
                   
                   {
                       switch (state)
                       {
                               
                           case SessionRefreshed:
                           {
                               
                               [self  updateState:@"登录完成."];
                               // 移除遮盖
                               [MBProgressHUD hideHUD];
                               //获取所有摄像头
                               [self loadAllCamera];
                                 NSLog(@"-->%d",self.self.m_camraList.count);

                               break;
                               
                           }
                               
                               
                           case SessionEncounteredError:
                           {
                               
                               
                               NSString * msg = nil;
                               
                               switch (lparam)
                               {
                                   case SessionNetworkFailed:
                                       
                                       
                                       msg = @"无法连接到服务器，请检查网络是否工作正常。";
                                        [self  updateState:msg];
                                       break;
                                       
                                   case SessionLoginFailed:
                                       
                                       msg = [self GetErrorDesc:wparam];
                                       [self  updateState:msg];
                                       break;
                                   case SessionFailed:
                                       msg = @"出现SessionFailed错误，请和开发商联系。";
                                       [self  updateState:msg];
                                       break;
                                   case SessionExpire:
                                       
                                       msg = @"用户已经过期，请进行续费处理。";
                                       [self  updateState:msg];
                                       break;
                                   case SessionOtherFailed:
                                       
                                       msg = @"出现无法预知的错误，请和开发商联系。";
                                       [self  updateState:msg];
                                       
                                       break;
                                   default:
                                       msg = @"出现未知的错误，请和开发商联系。";
                                       [self  updateState:msg];
                                       break;
                               }
                               
                               [self.m_session logout];
                               self.m_session = nil;
//                               [self showInfo:msg];
//                               
//                               
                               [self updateState:@"出现错误!"];
                               break;
                           }
                           default:
                               [self updateState:@"出现错误!"];
                               break;
                       }
                       
                   });
    
    
}

-(void)updateState:(NSString*)s
{
    
//    labelState.text = s;
    [MBProgressHUD hideHUD];
    
    NSLog(@"错误提示---》%@",s);
}

//登录平台
-(void)login
{
    // 显示一个蒙版(遮盖)
    [MBProgressHUD showMessage:@"登录中...."];
    self.m_session =[[AncdaSession alloc] init];
    //设置服务器地址
    self.m_session.serverAddress=@"d3.ancda.net";
    self.m_session.serverPort = 8116;
    //设置用户名密码
    self.m_session.userName = @"jinyaoshi";
    self.m_session.password = @"123456";
    self.m_session.delegate = self;
    [self.m_session login];
}

//注销平台


-(void)logoOut
{
    
    if (self.m_session)
    {
        [self.m_session logout];
        
        self.m_session = nil;
    }
    
    
    [self.self.m_camraList removeAllObjects];
    
    
}
-(void)dealloc
{
    
    [self logoOut];
}


@end
